package core.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageInfo;
import core.domain.ListDataInfo;
import core.domain.Result;
import core.enums.ResultCode;

import java.util.List;

public class BaseController {
    public Result<Void> toResult(int result) {
        return result > 0 ? Result.success(null) : Result.fail(ResultCode.FAILED);
    }

    public Result<Void> toResult(boolean result) {
        return result ? Result.success(null) : Result.fail(ResultCode.FAILED);
    }

    public ListDataInfo toListDataInfo(List<?> list) {
        if(CollectionUtil.isEmpty(list)) {
            return ListDataInfo.empty();
        }
        long total = new PageInfo<>(list).getTotal();
        return ListDataInfo.success(list, (int) total);
    }
}

